#include "Program.h"

#include <compilation/task/PrintTask.h>


namespace compilation
{


void
Program::print(std::ostream &out) const
{
  for (std::set<Function>::const_iterator functionIter = m_functions.begin();
       functionIter != m_functions.end(); ++functionIter)
    functionIter->print(out, std::function<void(void)>());
}


void
Program::printWithWorkList(std::ostream& out) const
{
  UNSET_DEBUG_FOR_VISITORS();

  if (debug_instruction_visitor)
    std::cout
        << "printWithWorkList:\n"
           "------------------\n";

  PrintAgenda::instruction_handler_visitor_type phv =
      PrintAgenda::instruction_handler_visitor_type
      ::build_visitor<PrintHandler>();

  PrintAgenda::instruction_propagator_visitor_type ppv =
      PrintAgenda::instruction_propagator_visitor_type
      ::build_visitor<DefaultInstructionPropagator
      <PrintTask, PrintAgenda>::type>();

  for (std::set<Function>::const_iterator functionIter = m_functions.begin();
       functionIter != m_functions.end(); ++functionIter)
  {
    out << "function " << functionIter->getName() << std::endl;
    PrintAgenda agenda(phv, ppv, out, *functionIter);
    execute(agenda);
    out << "      }" << std::endl;
    out << std::endl;
  }
}


} // compilation
